package com.store.app.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.store.app.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author shanquan.li
 * @email shanquanlee@163.com
 * @version 2013-7-26
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ViewPagerIndicator extends LinearLayout {

    private Context mContext;//上下文

    private int indicatorImageWidth;// indicatorImageWidth

    private int indicatorImageHeight;// indicatorImageHeight

    private int indicatorMargin;// indicatorMargin

    private Drawable indicatorNormalDrawable;// indicatorNormalDrawable

    private Drawable indicatorSelectedDrawable;// indicatorSelectedDrawable

    private int indicatorCount = 0;//指示数量

    private int currentPositoin = 0;//当前选中的下标

    private List<ImageView> indicatorViews = new ArrayList<ImageView>();// 保存指示图

    public ViewPagerIndicator(Context context) {
        super(context);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 保存上下文
        mContext = context;

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CustomIndicator);
        indicatorMargin = (int) typedArray.getDimension(R.styleable.CustomIndicator_margin, 0);
        indicatorImageWidth = (int) typedArray.getDimension(R.styleable.CustomIndicator_width, 0);
        indicatorImageHeight = (int) typedArray.getDimension(R.styleable.CustomIndicator_height1, 0);
        indicatorCount = typedArray.getInteger(R.styleable.CustomIndicator_count, 0);
        indicatorNormalDrawable = typedArray.getDrawable(R.styleable.CustomIndicator_normal_icon);
        indicatorSelectedDrawable = typedArray
                .getDrawable(R.styleable.CustomIndicator_selected_icon);
        typedArray.recycle();

        if (indicatorCount != 0) {
            // 初始化View
            initViews();
        }
    }

    /**
     * 设置当前选中的指示
     * 
     * @param position
     */
    public void setCurrentPosition(int position) {
        currentPositoin = position;

        if (currentPositoin < 0) {
            currentPositoin = 0;
        }

        if (currentPositoin > indicatorCount - 1) {
            currentPositoin = indicatorCount - 1;
        }

        for (int i = 0; i < indicatorCount; i++) {
            indicatorViews.get(i).setBackgroundDrawable(indicatorNormalDrawable);
        }

        indicatorViews.get(currentPositoin).setBackgroundDrawable(indicatorSelectedDrawable);
    }

    /**
     * 显示下一个指示
     */
    public void next() {
        setCurrentPosition(currentPositoin + 1);
    }

    /**
     * 显示上一个指示
     */
    public void previous() {
        setCurrentPosition(currentPositoin - 1);
    }

    /**
     * 设置统计数量
     * 
     * @param count
     */
    public void setCount(int count) {
        this.indicatorCount = count;
        this.currentPositoin = 0;
    }

    /**
     * 初始化View
     */
    public void initViews() {

        if (indicatorViews != null) {
            indicatorViews.clear();
        }

        if (indicatorCount == 0) {
            return;
        }

        for (int i = 0; i < indicatorCount; i++) {
            ImageView view = new ImageView(mContext);
            indicatorViews.add(view);
            LayoutParams params = new LayoutParams(
                    indicatorImageWidth == 0 ? LayoutParams.WRAP_CONTENT : indicatorImageWidth,
                    indicatorImageHeight == 0 ? LayoutParams.WRAP_CONTENT : indicatorImageHeight);
            if (i != indicatorCount - 1) {
                params.rightMargin = indicatorMargin;
            }
            view.setLayoutParams(params);
            view.setBackgroundDrawable(indicatorNormalDrawable);
            this.addView(view);
        }

        setCurrentPosition(0);
    }
}
